package top.whysu.gps.service.system.impl;

import org.springframework.stereotype.Service;
import top.whysu.gps.constant.Constant;
import top.whysu.gps.dao.system.DataDictionaryDao;
import top.whysu.gps.po.system.DataDictionaryPO;
import top.whysu.gps.service.system.DataDictionaryDTO;
import top.whysu.gps.service.system.DataDictionaryService;
import top.whysu.gps.service.system.LabelDTO;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 数据字典服务接口类
 */
@Service("DataDictionaryServiceImpl")
public class DataDictionaryServiceImpl implements DataDictionaryService {

    @Resource
    private DataDictionaryDao dataDictionaryDao;

    @Override
    public List<DataDictionaryDTO> selectList() {
        //获得所有的语言为简体中文的字典数据
        Map<String, Object> params = new HashMap<>();
        params.put("lan", Constant.LANGUAGE_ZH_CN);
        List<DataDictionaryPO> lists = dataDictionaryDao.selectList(params);

        //按照DataDictionaryPO的dataType分好了组, 类似:
        // { 1=[DataDictionaryPO{id=1, dataType=1, value=1, lan='zh_CN', label='是'},
        // DataDictionaryPO{id=2, dataType=1, value=2, lan='zh_CN', label='否'}] ...}
        Map<Integer, List<DataDictionaryPO>> map = lists.stream().collect(Collectors.groupingBy(DataDictionaryPO::getDataType));
        //遍历map, 转为 List<DataDictionaryDTO>格式
        List<DataDictionaryDTO> dtos = new ArrayList<>();
        for (Map.Entry<Integer, List<DataDictionaryPO>> entry : map.entrySet()) {
            DataDictionaryDTO dto = new DataDictionaryDTO();
            dto.setDataType(entry.getKey());
            List<LabelDTO> labelList = new ArrayList<>();
            for (DataDictionaryPO po : entry.getValue()) {
                LabelDTO labelDTO = new LabelDTO();
                labelDTO.setValue(po.getValue());
                labelDTO.setLabel(po.getLabel());
                labelList.add(labelDTO);
            }
            dto.setLabels(labelList);
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public Map<Integer, List<DataDictionaryPO>> selectLists() {
        //获得所有的语言为简体中文的字典数据
        Map<String, Object> params = new HashMap<>();
        params.put("lan", Constant.LANGUAGE_ZH_CN);
        List<DataDictionaryPO> lists = dataDictionaryDao.selectList(params);
        Map<Integer, List<DataDictionaryPO>> map = lists.stream().collect(Collectors.groupingBy(DataDictionaryPO::getDataType));
        return map;
    }

}
